package com.yc.exception;

import com.yc.web.controller.model.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * 统一处理控制器层抛出的各种异常，返回规范的错误响应
 */
@RestControllerAdvice
public class GlobalExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    // 处理用户不存在异常
    @ExceptionHandler(UserNotFoundException.class)
    public ResponseEntity<ResponseResult> handleUserNotFoundException(UserNotFoundException ex) {
        logger.warn("用户不存在异常：{}", ex.getMessage());
        return ResponseEntity.status(ex.getHttpStatus())
                .body(ResponseResult.error(ex.getMessage()));
    }

    // 处理用户名已存在异常
    @ExceptionHandler(UsernameAlreadyExistsException.class)
    public ResponseEntity<ResponseResult> handleUsernameAlreadyExistsException(UsernameAlreadyExistsException ex) {
        logger.warn("用户名已存在异常：{}", ex.getMessage());
        return ResponseEntity.status(ex.getHttpStatus())
                .body(ResponseResult.error(ex.getMessage()));
    }

    // 处理认证失败异常
    @ExceptionHandler(AuthenticationFailedException.class)
    public ResponseEntity<ResponseResult> handleAuthenticationFailedException(AuthenticationFailedException ex) {
        logger.warn("认证失败异常：{}", ex.getMessage());
        return ResponseEntity.status(ex.getHttpStatus())
                .body(ResponseResult.error(ex.getMessage()));
    }

    // 处理无效参数异常
    @ExceptionHandler(InvalidParameterException.class)
    public ResponseEntity<ResponseResult> handleInvalidParameterException(InvalidParameterException ex) {
        logger.warn("无效参数异常：{}", ex.getMessage());
        return ResponseEntity.status(ex.getHttpStatus())
                .body(ResponseResult.error(ex.getMessage()));
    }

    // 处理无效密码异常
    @ExceptionHandler(InvalidPasswordException.class)
    public ResponseEntity<ResponseResult> handleInvalidPasswordException(InvalidPasswordException ex) {
        logger.warn("无效密码异常：{}", ex.getMessage());
        return ResponseEntity.status(ex.getHttpStatus())
                .body(ResponseResult.error(ex.getMessage()));
    }

    // 处理系统操作异常
    @ExceptionHandler(SystemOperationException.class)
    public ResponseEntity<ResponseResult> handleSystemOperationException(SystemOperationException ex) {
        if (ex.getCause() != null) {
            logger.error("系统操作异常：{}", ex.getMessage(), ex);
        } else {
            logger.error("系统操作异常：{}", ex.getMessage());
        }
        return ResponseEntity.status(ex.getHttpStatus())
                .body(ResponseResult.error(ex.getMessage()));
    }

    // 处理所有未捕获的异常，作为兜底
    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseResult> handleGenericException(Exception ex) {
        logger.error("未捕获的异常：{}", ex.getMessage(), ex);
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                .body(ResponseResult.error("系统繁忙，请稍后再试"));
    }
}